'use strict';

const pkg = require('../package.json')
const log = require('@geek-cli/log')
const semver = require('semver')
const colors = require('colors/safe')
const rootCheck = require('root-check')
const userHome = require('user-home')
const pathExists = require('path-exists').sync
const constant = require('./const')

module.exports = core;

function core() {
  try {
    checkPkgVersion()
    checkNodeVersion()
    checkRoot()
    checkUserHome()
  } catch (error) {
    log.error(error.message)
  }
  
}

function checkNodeVersion(){
  // 1.获取当前Node版本号
  const currentNodeVersion = process.version
  // 2.比对最低版本号 
  const lowestNodeVersion = constant.LOWEST_NODE_VERSION

  if(!semver.gte(currentNodeVersion, lowestNodeVersion)) {
    throw new Error(colors.red(`geek-cli 需要安装 v${lowestNodeVersion}以上版本的 Node.js`))
  }
}

function checkPkgVersion() {
  log.info('version', pkg.version)
}

function checkRoot(){
  rootCheck()
}

function checkUserHome() {
  if(!userHome || !pathExists(userHome)) {
    throw new Error(colors.red('当前登录用户主目录不存在'))
  }
}

